在C++中写了一个简单的函数如下:
头文件:
1 | void __declspec(dllexport) GetUnicodeString(wstring& str); |
实现文件:
1 | void GetUnicodeString(wstring& str) |
在C#中,通过Dllimport的方式引入:
1 | [DllImport(@"Test.dll", |
遗憾的是在调用GetUnicodeString(ref str);的时候崩溃了,按理说C#和C++是两个语言,这样用string传入风险值很大,但是竟然有个同事一直这样用了很久。。。难以理解。
要想C#调用C++的函数,只能用原生态的参数(char,wchar_t),故把C++的函数改为:
1 | void GetUnicodeString(wchar_t* wstr, int len) |
然后C#在使用时,以Byte申请空间传入,最后转为Unicode字符串
1 | public static extern void GetUnicodeString(ref Byte str, int len); |
终于,得到了返回的字符串值This is a String in C++ Dll
。